(function(_scope){
	/*
	 * pi.layer.js
	 * Draggable&Resizeable layer plug-in for pi.js
	 * 1.0
	 * Azer Koçulu <http://azer.kodfabrik.com>
	 * http://pi.kodfabrik.com
	 */
	_scope.layer = pi.base.extend({
		"$Init":function(_element,_dragTrigger,_resizeTrigger){
			this.environment.setElement(_element).environment.setDragTrigger(_dragTrigger.event.addListener("mousedown",this.event.mousedown.curry(this,true))).environment.setResizeTrigger(_resizeTrigger.event.addListener("mousedown",this.event.mousedown.curry(this,false)));
			pi.util.AddEvent(pi.env.ie?document:window,"mousemove",this.event.mousemove.curry(this))("mouseup",this.event.mouseup.curry(this));
			return this;
		},
		"environment":{
			"_confirmationFunction":false, "coordinates": { x: [], y:[] }, "_dragTrigger":null, "_element":null, "_height":0, "_left":0, "_resizeTrigger":null, "ondrag":false, "onresize":false, "_top":0, "_trigger":null, "_width":0,
			"css":["left","top","width","height"],
			"setHeight":function(_value){
				this.getElement().element.style[ this.css[3] ] = _value+"px"
				this._setHeight(_value);
				return this._parent_;
			},
			"setLeft":function(_value){
				this.getElement().element.style[ this.css[0] ] = _value+"px"
				this._setLeft(_value);
				return this._parent_;
			},
			"setTop":function(_value){
				this.getElement().element.style[ this.css[1] ] = _value+"px"
				this._setTop(_value);
				return this._parent_;
			},
			"setWidth":function(_value){
				this.getElement().element.style[ this.css[2] ] = _value+"px"
				this._setWidth(_value);
				return this._parent_;
			}
		},
		"event":{
			"mousedown":function(_dragging,_event){
				this.environment.coordinates.x = [null,_event.clientX+Math.max( document.documentElement.scrollLeft, document.body.scrollLeft )];
				this.environment.coordinates.y  = [null,_event.clientY+Math.max( document.documentElement.scrollTop, document.body.scrollTop )];
				
				if (_dragging) {
					this.environment.coordinates.x[0] = this.environment.getLeft();
					this.environment.coordinates.y[0] = this.environment.getTop();
					this.environment.ondrag = true;
				} else {
					this.environment.coordinates.x[0] = this.environment.getWidth();
					this.environment.coordinates.y[0] = this.environment.getHeight();
					this.environment.onresize = true;
				}
			},
			"mousemove":function(_event){
				
				if(!this.environment.onresize&&!this.environment.ondrag)
					return;
					
				this.environment.coordinates.x[2] = (_event.clientX+Math.max( document.documentElement.scrollLeft, document.body.scrollLeft ) )-this.environment.coordinates.x[1];
				this.environment.coordinates.y[2] =  (_event.clientY+Math.max( document.documentElement.scrollTop, document.body.scrollTop ) )-this.environment.coordinates.y[1];
				
				var x = this.environment.coordinates.x[0] + this.environment.coordinates.x[2], y = this.environment.coordinates.y[0] + this.environment.coordinates.y[2];
				
				if(this.environment.ondrag){
					if (!this.environment.getConfirmationFunction() || this.environment.getConfirmationFunction()(x, y, this.environment.getWidth(), this.environment.getHeight())) {
						this.environment.setTop(y).environment.setLeft(x);
						this.event.move(this);
					}
				} else {
					if (!this.environment.getConfirmationFunction() || this.environment.getConfirmationFunction()(this.environment.getLeft(), this.environment.getTop(), x, y)) {
						this.environment.setWidth(x).environment.setHeight(y);
						this.event.resize(this);
					}
				}
			},
			"mouseup":function(){
				this.environment.ondrag = false;
				this.environment.onresize = false;
			},
			"move":new Function,
			"resize":new Function
		}
	});
})(pi);